﻿using HalconDotNet;
using MPSizectorDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        MPDisplayer3D Displayer3D;

        MP3DFrameManaged Buffer3DLast;
        MP3DFrameManaged m;


        Bitmap Bitmap3DLast= new Bitmap(728,544);


        public Form1()
        {
            Displayer3D = new MPDisplayer3D();
            InitializeComponent();

            PicPnl.Controls.Add(Displayer3D);
            Displayer3D.Dock = DockStyle.Fill;

        }
        HObject ho_X, ho_Y, ho_Z;
        bool bl = false;
        private void button1_Click(object sender, EventArgs e)
        {         
            if (!bl)
            {
                button2_Click1();
                bl = true;
            }
            HOperatorSet.ReadImage(out ho_X, "./ho_X");
            HOperatorSet.ReadImage(out ho_Y, "./ho_Y");
            HOperatorSet.ReadImage(out ho_Z, "./ho_Z");
            F();


            Displayer3D.Visible = true;
            pb1.Image = m.GetColorDepthBitmap(Convert3DTargetType.Z, 10, -10);
            //Displayer3D.DeviceFOV = 500;
            try
            {
                //Displayer3D.DeviceFOV = Convert.ToInt32(tb1.Text);
            }
            catch
            {

                //Displayer3D.DeviceFOV = 600;
            }
            MP3DFrameManaged   DLast = new MP3DFrameManaged(m);

            //Displayer3D.Data3DManaged = DLast;
            //Displayer3D.Data3DManaged = Buffer3DLast;


            //Buffer3DLast.Save("abc.m3dm");


        }
        float[] X, Y, Z;
        private void button2_Click1()
        {
            Buffer3DLast = new MP3DFrameManaged("abc.m3dm");
            ////Displayer3D.DeviceFOV = 800;
            ////Displayer3D.
            Displayer3D.Data3DManaged = Buffer3DLast;
        }
        uint len;
        float[] X1;
        float[] Y1;
        float[] Z1;
        byte[] Gray;
        private void button2_Click(object sender, EventArgs e)
        {

            byte[] buf= {0x2c,0xa4,0x11,0x3d };
            float f=BitConverter.ToSingle(buf, 0);


            byte[] bufs = BitConverter.GetBytes(0.315566f);


            MP3DFrameManaged mc = new MP3DFrameManaged("xyz.m3dm");
            mc.PostProcessRemoveBurrs();
            mc.PostProcessMend();
            mc.PostProcessFiltrate();
            mc.PostProcessFiltrate();
            len = mc.SensorWidth * mc.SensorHeight;
            X1 = new float[len];
            Y1 = new float[len];
            Z1 = new float[len];
            Gray = new byte[len];
            ParallelLoopResult result =
            Parallel.For(0, mc.Data.Length, i =>
            {
                //
                if (float.IsNaN(mc.Data[i].X) | float.IsNaN(mc.Data[i].Y) | float.IsNaN(mc.Data[i].Z) | (mc.Data[i].Mask != 0))
                {
                    X1[i] = 0;
                    Y1[i] = 0;
                    Z1[i] = 0;
                }
                else
                {
                    X1[i] = mc.Data[i].X;
                    Y1[i] = mc.Data[i].Y;
                    Z1[i] = mc.Data[i].Z + 1000;
                }
                Gray[i] = mc.Data[i].Gray;
            });

        HObject XPicture = HObjectConvertFromBitmap((int)mc.SensorHeight, (int)mc.SensorWidth, Dat(X1));
        HObject YPicture = HObjectConvertFromBitmap((int)mc.SensorHeight, (int)mc.SensorWidth, Dat(Y1));
        HObject ZPicture = HObjectConvertFromBitmap((int)mc.SensorHeight, (int)mc.SensorWidth, Dat(Z1));
        HObject GrayPicture = HObjectConvertGray((int)mc.SensorHeight, (int)mc.SensorWidth, Datbyte(Gray));
            HOperatorSet.WriteImage(XPicture, "tiff", 0, "x");
            HOperatorSet.WriteImage(YPicture, "tiff", 0, "y");
            HOperatorSet.WriteImage(ZPicture, "tiff", 0, "z");
            HOperatorSet.WriteImage(GrayPicture, "tiff", 0, "gray");
            XPicture.Dispose();
            YPicture.Dispose();
            ZPicture.Dispose();
            GrayPicture.Dispose();

        }
        private unsafe IntPtr Dat(float[] fl)
        {
            void* p;
            IntPtr ptr;
            fixed (float* pc = fl)
            {

                p = (void*)pc;
                ptr = new IntPtr(p);
            }
            return ptr;
        }
        private unsafe IntPtr Datbyte(byte[] fl)
        {
            void* p;
            IntPtr ptr;
            fixed (byte* pc = fl)
            {

                p = (void*)pc;
                ptr = new IntPtr(p);
            }
            return ptr;
        }
        public static HObject HObjectConvertFromBitmap(int height, int width, IntPtr ip)
        {
            try
            {
                HObject ho_Image;
                HOperatorSet.GenEmptyObj(out ho_Image);
                HOperatorSet.GenImage1(out ho_Image, "real", width, height, ip);
                return ho_Image;
            }
            catch (Exception e)
            {
                MessageBox.Show("拍照数据错误");
                return null;
            }
        }
        public static HObject HObjectConvertGray(int height, int width, IntPtr ip)
        {
            try
            {
                HObject ho_Image;
                HOperatorSet.GenEmptyObj(out ho_Image);
                HOperatorSet.GenImage1(out ho_Image, "byte", width, height, ip);
                return ho_Image;
            }
            catch (Exception e)
            {
                MessageBox.Show("拍照数据错误");
                return null;
            }
        }
        private void F()
        {
            HTuple s1 = new HTuple();
            HTuple s2 = new HTuple();
            HTuple s3 = new HTuple();
            HTuple s4 = new HTuple();

            HTuple a = new HTuple();
            HTuple b = new HTuple();
            HTuple c = new HTuple();

            HOperatorSet.GetImagePointer1(ho_X, out a, out s2, out s3, out s4);
            X = new float[s3 * s4];
            Marshal.Copy((IntPtr)a, X, 0, s3 * s4);
            //rite(X, "x");

            HOperatorSet.GetImagePointer1(ho_Y, out b, out s2, out s3, out s4);
            Y = new float[s3 * s4];
            Marshal.Copy((IntPtr)b, Y, 0, s3 * s4);
            //rite(Y, "y");

            HOperatorSet.GetImagePointer1(ho_Z, out c, out s2, out s3, out s4);
            Z = new float[s3 * s4];
            Marshal.Copy((IntPtr)c, Z, 0, s3 * s4);
            //rite(Z, "z");

            m = new MP3DFrameManaged((uint)s3, (uint)s4);


            ParallelLoopResult result =
            Parallel.For(0, s3 * s4, i =>
            {
                if (X[i]==0)
                {
                    m.Data[i].X = float.NaN;
                    m.Data[i].Y = float.NaN;
                    m.Data[i].Z = float.NaN;
                }
                else
                {
                    m.Data[i].X = X[i];
                    m.Data[i].Y = Y[i];
                    m.Data[i].Z = Z[i];
                }
            });
        }
        private void rite(float[] f, string strc)
        {
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < f.Length; i++)
            {
                str.Append(f[i].ToString() + "__");
            }
            FileStream fs = new FileStream(strc+".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter ab = new StreamWriter(fs); // 创建写入流
            ab.WriteLine(str.ToString()); // 写入Hello World
            ab.Close(); //关闭文件
        }



        //this.Invoke(new Action(() => { Displayer3D.Data3DManaged = Buffer3DLast; Displayer3D.Visible = true;  }));



    }
}
